package com.jcfk.eam.service.mould;

import cn.hutool.core.util.StrUtil;
import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jcfk.common.business.service.BaseService;
import com.jcfk.common.core.utils.BeanUtils;
import com.jcfk.common.security.utils.SecurityUtils;
import com.jcfk.common.web.domain.DataPage;
import com.jcfk.common.web.domain.RetResult;
import com.jcfk.eam.dao.mould.EamMouldEmailConfigDao;
import com.jcfk.eam.domain.dto.mould.EamMouldEmailConfigDTO;
import com.jcfk.eam.domain.po.mould.EamMouldEmailConfig;
import com.jcfk.eam.domain.query.mould.EamMouldEmailConfigQuery;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

import static com.jcfk.common.core.utils.BeanUtils.convertList;

/**
 * 模具邮件发送配置表
 *
 * @author yrl
 * @email
 * @date 2025-07-25
 */
@Service
public class EamMouldEmailConfigService extends BaseService<EamMouldEmailConfigDao, EamMouldEmailConfig> {

    /**
     * EamMouldEmailConfig分页查询
     *
     * @param page  分页设置
     * @param query 查询条件
     * @return 分页查询结果
     */
    public DataPage<EamMouldEmailConfigDTO> queryByPage(DataPage<EamMouldEmailConfigDTO> page, EamMouldEmailConfigQuery query) {
        // 分页查询
        query.setSendRoleId(StringUtils.isEmpty(query.getSendRoleId()) ? query.getSendRoleId() : "%" + query.getSendRoleId() + "%");
        query.setSendRoleName(StringUtils.isEmpty(query.getSendRoleName()) ? query.getSendRoleName() : query.getSendRoleName() + "%");
        page = this.baseMapper.queryByPage(page, query);
        return page;
    }


    /**
     * 判断EamMouldEmailConfig是已经存在
     *
     * @param dto
     * @return
     */
    public RetResult<String> isValid(EamMouldEmailConfigDTO dto) {
        QueryWrapper<EamMouldEmailConfig> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted", 0);
        queryWrapper.lambda().eq(EamMouldEmailConfig::getProductId, dto.getProductId());
        queryWrapper.lambda().eq(EamMouldEmailConfig::getSendStage, dto.getSendStage());
        if (!StringUtils.isEmpty(dto.getId())) queryWrapper.ne("id", dto.getId());
        Integer count = this.count(queryWrapper);

        if (count > 0) return RetResult.fail("该产品已存在相同阶段的配置");

        return RetResult.ok();
    }

    /**
     * 插入模具邮件发送配置表
     *
     * @param dto 插入对象
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void insert(EamMouldEmailConfigDTO dto) {

        // 保存
        EamMouldEmailConfig item = new EamMouldEmailConfig();
        BeanUtils.copyProperties(dto, item);
        item.setEnabled(1);
        this.setDefaultValue(item);

        super.save(item);
    }

    /**
     * 更新模具邮件发送配置表
     *
     * @param dto 更新对象
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void update(EamMouldEmailConfigDTO dto) {

        // 保存
        EamMouldEmailConfig item = new EamMouldEmailConfig();
        BeanUtils.copyProperties(dto, item);

        // 默认数据
        this.setDefaultValue(item);

        super.updateById(item);
    }

    /**
     * 根据批量主键删除模具邮件发送配置表(逻辑删除)
     *
     * @param dto
     * @return bool
     */
    @Transactional(rollbackFor = Exception.class)
    public Boolean logicDeleteList(EamMouldEmailConfigDTO dto) {

        EamMouldEmailConfig item = new EamMouldEmailConfig();

        this.setDefaultValue(item);
        item.setDeleted(1);

        QueryWrapper<EamMouldEmailConfig> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", dto.getIds());

        return this.update(item, queryWrapper);
    }

    /**
     * 设置添加默认值
     *
     * @param item 添加对象
     */
    public void setDefaultValue(EamMouldEmailConfig item) {
        if (StrUtil.isEmpty(item.getCreatedUserId())) {
            item.setCreatedUserId(SecurityUtils.getUserId());
            item.setCreateTime(new Date());
            item.setDeleted(0);
            item.setCreatedUserName(SecurityUtils.getUserName());
        }
        item.setLastUpdatedUserId(SecurityUtils.getUserId());
        item.setLastUpdateTime(new Date());

        item.setLastUpdatedUserName(SecurityUtils.getUserName());

    }

    /**
     * 根据主键获取模具邮件发送配置表信息
     *
     * @param id 主键
     * @return StandardCodeDTO
     */
    public EamMouldEmailConfigDTO get(String id) {
        // 查询数据
        EamMouldEmailConfig item = this.baseMapper.selectById(id);
        EamMouldEmailConfigDTO dto = new EamMouldEmailConfigDTO();
        try {
            BeanUtils.copyProperties(item, dto);
        } catch (RuntimeException e) {
            throw e;
        }

        return dto;
    }


    /**
     * 根据条件获取单表信息列表
     *
     * @param query 查询条件
     * @return List<EamMouldEmailConfigDTO>
     */
    public List<EamMouldEmailConfigDTO> getList(EamMouldEmailConfigQuery query) {
        // 查询条件
        QueryWrapper<EamMouldEmailConfig> qw = new QueryWrapper<>();
        qw.lambda().eq(query.getSendStage() != null, EamMouldEmailConfig::getSendStage, query.getSendStage());
        qw.lambda().likeRight(!StrUtil.isEmpty(query.getSendRoleId()), EamMouldEmailConfig::getSendRoleId, query.getSendRoleId());
        qw.lambda().likeRight(!StrUtil.isEmpty(query.getSendRoleName()), EamMouldEmailConfig::getSendRoleName, query.getSendRoleName());
        qw.lambda().eq(!StrUtil.isEmpty(query.getProductId()), EamMouldEmailConfig::getProductId, query.getProductId());
        // 查询数据
        List<EamMouldEmailConfig> list = super.list(qw);

        return convertList(list, EamMouldEmailConfigDTO.class);
    }

    /**
     * 根据主键获取模具邮件发送配置表信息
     *
     * @param id 主键
     * @return StandardCodeDTO
     */
    public void deleteItem(String id) {
        EamMouldEmailConfig item = getById(id);
        this.setDefaultValue(item);
        item.setDeleted(1);

        this.updateById(item);
    }
}

